﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class ModelHelperDemo : Form
    {
        public ModelHelperDemo()
        {
            InitializeComponent();
        }
        Cocon90.Lib.Dal.Tools.ModelHelper<Model.Address> mh = new Cocon90.Lib.Dal.Tools.ModelHelper<Model.Address>();
        private void button1_Click(object sender, EventArgs e)
        {
            var list = mh.GetList("select top 100 * from Person.Address where AddressLine1 is not null");
            comboBox1.DataSource = list;
            comboBox1.DisplayMember = "AddressLine1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lbPrimeryKey.Text = "当前Model对应主键：" + mh.GetPrimeryKey();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            lbGetTableName.Text = "当前Model对应的表名是：" + mh.GetTableName();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            lbGetRowCount.Text = "当前Model对应的数据量是：" + mh.GetRowCount();
            lbGetRowCount.Text += "\r\n自定义Where子句后数据量是：" + mh.GetRowCount("AddressLine2 is not null");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Model.Address ad = new Model.Address();
            cbColumns.DataSource = mh.GetColumnNames(ad, true); //第二参数true表示将ad中对应为null值的列也映射过去。

        }

        private void button6_Click(object sender, EventArgs e)
        {
            lbRowCount.Text = "添加前数据行数：" + mh.GetRowCount();

            Model.Address newModel = new Model.Address();
            newModel.AddressLine1 = "ShangXi" + DateTime.Now.Millisecond;
            newModel.AddressLine2 = "LvLiang" + DateTime.Now.Millisecond;
            newModel.City = "ShangXi" + DateTime.Now.Millisecond;
            newModel.ModifiedDate = DateTime.Now;
            newModel.PostalCode = "033600";
            newModel.rowguid = Guid.NewGuid();
            newModel.StateProvinceID = 76;
            bool succ = mh.Add(newModel);

            lbRowCount.Text += "\r\n添加后数据行数：" + mh.GetRowCount();
            dgv.DataSource = mh.GetList("select top 100 * from Person.Address order by AddressID desc");
            MessageBox.Show(succ ? "添加成功！" : "添加失败");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Model.Address newModel = new Model.Address();
            newModel.AddressLine1 = "ShangXi" + DateTime.Now.Millisecond;
            newModel.AddressLine2 = "LvLiang" + DateTime.Now.Millisecond;
            newModel.City = "ShangXi" + DateTime.Now.Millisecond;
            newModel.ModifiedDate = DateTime.Now;
            newModel.PostalCode = "033600";
            newModel.rowguid = Guid.NewGuid();
            newModel.StateProvinceID = 76;
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=10");
            bool succ = mh.EditByPrimeryKey(newModel, 10);
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=10");
            MessageBox.Show(succ ? "修改成功！" : "修改失败");

        }

        private void button8_Click(object sender, EventArgs e)
        {
            Model.Address newModel = new Model.Address();
            newModel.AddressLine1 = "ShangXi" + DateTime.Now.Millisecond;
            newModel.AddressLine2 = "LvLiang" + DateTime.Now.Millisecond;
            newModel.City = "ShangXi" + DateTime.Now.Millisecond;
            newModel.ModifiedDate = DateTime.Now;
            newModel.PostalCode = "033600";
            newModel.rowguid = Guid.NewGuid();
            newModel.StateProvinceID = 76;
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");

            bool succ = mh.EditByCustomWhere(newModel, "AddressID=12");

            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");
            MessageBox.Show(succ ? "修改成功！" : "修改失败");
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");
            try
            {
                bool succ = mh.DeleteByPrimeryKey(12);
                MessageBox.Show(succ ? "删除成功！" : "删除失败");
            }
            catch (Exception ex) { MessageBox.Show("语句正常，但是存在其它约束，数据库不允许删除：" + ex.Message); }
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");
        }

        private void button8_Click_1(object sender, EventArgs e)
        {
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");
            try
            {
                bool succ = mh.DeleteByModel(new Model.Address { AddressID = 12 });
                MessageBox.Show(succ ? "删除成功！" : "删除失败");
            }
            catch (Exception ex) { MessageBox.Show("语句正常，但是存在其它约束，数据库不允许删除：" + ex.Message); }
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");
            try
            {
                bool succ = mh.DeleteByCustomWhere("AddressID=12");
                MessageBox.Show(succ ? "删除成功！" : "删除失败");
            }
            catch (Exception ex) { MessageBox.Show("语句正常，但是存在其它约束，数据库不允许删除：" + ex.Message); }
            dgv.DataSource = mh.GetList("select * from Person.Address where AddressID=12");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Model.Address newModel = new Model.Address();
            newModel.AddressLine1 = "ShangXi";
            newModel.AddressLine2 = "LvLiang";
            newModel.City = "ShangXi";
            //newModel.ModifiedDate = DateTime.Now;
            newModel.PostalCode = "033600";
            //newModel.rowguid = Guid.NewGuid();
            newModel.StateProvinceID = 76;

            Model.Address newModel2 = new Model.Address();
            newModel2.AddressLine1 = "ShangXi";
            newModel2.AddressLine2 = "LvLiang";
            newModel2.City = "ShangXi";
            //newModel2.ModifiedDate = DateTime.Now;
            newModel2.PostalCode = "033600";
            //newModel2.rowguid = Guid.NewGuid();
            newModel2.StateProvinceID = 76;
            MessageBox.Show("newModel和newModel2是否相同：" + mh.IsModelEqual(newModel, newModel2));
        }



    }
}
